#include "ThreadCond.hpp"
using namespace std;
int num =10000;
void *ticket(void *args)
{
    //Mutex *mutex = static_cast<Mutex *>(args);
    while (true)
    {
        // 上锁
        //pthread_mutex_lock(&mutex->GetMutex());
         Steward *steward = static_cast<Steward *>(args);
        if (num > 0)
        {
            num--;
            cout << "线程抢到一张票，剩余票数:" << num << endl;
            //pthread_mutex_unlock(&mutex->GetMutex());
        }
        else
        {
            //pthread_mutex_unlock(&mutex->_mutex);
            break;
        }
    }
    return nullptr;
}
int main()
{
    pthread_t t1, t2, t3, t4;

    // 锁
    Mutex m;
    if (pthread_create(&t1, nullptr, ticket, &m) != 0)
    {
        cout << "创建失败" << endl;
        return 1;
    }
    if (pthread_create(&t2, nullptr, ticket, &m) != 0)
    {
        cout << "创建失败" << endl;
        return 1;
    }
    if (pthread_create(&t3, nullptr, ticket, &m) != 0)
    {
        cout << "创建失败" << endl;
        return 1;
    }
    if (pthread_create(&t4, nullptr, ticket, &m) != 0)
    {
        cout << "创建失败" << endl;
        return 1;
    }

    pthread_join(t1, nullptr);
    pthread_join(t2, nullptr);
    pthread_join(t3, nullptr);
    pthread_join(t4, nullptr);
    return 0;
}